<!DOCTYPE html>
<html lang="zh-CN">

<head>
    <meta charset="UTF-8">
    <meta name="viewport" content="width=device-width, initial-scale=1.0">
    <title>数据管理列表</title>
    <style>
        * {
            margin: 0;
            padding: 0;
            box-sizing: border-box;
            font-family: 'Segoe UI', Tahoma, Geneva, Verdana, sans-serif;
        }

        body {
            background-color: #f5f7fa;
            padding: 20px;
        }

        .container {
            max-width: 1000px;
            margin: 0 auto;
            background-color: white;
            border-radius: 8px;
            box-shadow: 0 2px 10px rgba(0, 0, 0, 0.05);
            overflow: hidden;
        }

        .header {
            display: flex;
            justify-content: space-between;
            align-items: center;
            padding: 16px 20px;
            border-bottom: 1px solid #eee;
        }

        .header h1 {
            font-size: 1.5rem;
            color: #333;
            font-weight: 600;
        }

        .add-btn {
            background-color: #4285f4;
            color: white;
            border: none;
            padding: 8px 16px;
            border-radius: 4px;
            cursor: pointer;
            font-size: 0.9rem;
            display: flex;
            align-items: center;
            gap: 6px;
            transition: background-color 0.2s;
        }

        .add-btn:hover {
            background-color: #3367d6;
        }

        table {
            width: 100%;
            border-collapse: collapse;
        }

        th,
        td {
            padding: 12px 20px;
            text-align: left;
        }

        th {
            background-color: #f8f9fa;
            color: #5f6368;
            font-weight: 500;
            font-size: 0.9rem;
            border-bottom: 1px solid #eee;
        }

        td {
            color: #333;
            font-size: 0.9rem;
            border-bottom: 1px solid #eee;
        }

        tr:hover td {
            background-color: #fafafa;
        }

        .action-btn {
            display: flex;
            gap: 8px;
        }

        .edit-btn,
        .delete-btn {
            padding: 4px 10px;
            border-radius: 4px;
            font-size: 0.8rem;
            cursor: pointer;
            transition: all 0.2s;
            border: none;
        }

        .edit-btn {
            background-color: #34a853;
            color: white;
        }

        .edit-btn:hover {
            background-color: #2d8643;
        }

        .delete-btn {
            background-color: #ea4335;
            color: white;
        }

        .delete-btn:hover {
            background-color: #d93025;
        }

        /* 响应式调整 */
        @media (max-width: 768px) {
            .header {
                flex-direction: column;
                align-items: flex-start;
                gap: 12px;
            }

            th,
            td {
                padding: 10px 12px;
            }

            .action-btn {
                flex-direction: column;
            }
        }
    </style>
    <script src="../axios/axios.min.js"></script>
    <script src="../axios/form-serialize.js"></script>
    <link rel="stylesheet" href="../axios/bootstrap.css">
    <script src="../axios/bootstrap.bundle.js"></script>
</head>

<body>
    <div class="container">
        <div class="header">
            <h1>数据管理</h1>
            <button type="button" class="btn btn-primary add-btn" data-bs-toggle="modal" data-bs-target="#exampleModal">
                +新增部门
            </button>
            <!-- <button type="button" class="btn btn-search add-btn" data-bs-toggle="modal" data-bs-target="#exampleModal">
                +查询部门
            </button> -->
        </div>
        <table>
            <thead>
                <tr>
                    <th>序号</th>
                    <th>部门名称</th>
                    <th>修改时间</th>
                    <th>操作</th>
                </tr>
            </thead>
            <tbody class="body">
                <tr>
                    <td>1</td>
                    <td>2023-10-01 09:30</td>
                    <td>2023-10-01 10:15</td>
                    <td class="action-btn" id="">
                        <button class="edit-btn" data-bs-toggle="modal" data-bs-target="#exampleModal1">修改</button>
                        <button class="delete-btn">删除</button>
                    </td>
                </tr>
            </tbody>
        </table>

        <!-- Modal -->
        <!-- 修改Modal部分 -->
        <div class="modal fade" id="exampleModal" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h1 class="modal-title fs-5" id="exampleModalLabel">部门管理</h1>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <form class="login-form">
                        <input type="hidden" class="id">
                        部门名称:<input type="text" name="deptname" class="deptname"><br>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关鸡</button>
                        <button type="button" class="btn btn-primary zouni1">走你</button>
                    </div>
                </div>
            </div>
        </div>
        <button type="button" class="btn btn-primary search-btn" data-bs-toggle="modal" data-bs-target="#exampleModal0">
            +查询部门
        </button>

        <div class="modal fade" id="exampleModal0" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h1 class="modal-title fs-5" id="exampleModalLabel">部门管理</h1>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <form class="login-form">
                        <input type="hidden" class="id">
                        部门id:<input type="text" name="id" class="id-"><br>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关鸡</button>
                        <button type="button" class="btn btn-primary zouni1">走你</button>
                    </div>
                </div>
            </div>
        </div>

        <div class="modal fade" id="exampleModal1" tabindex="-1" aria-labelledby="exampleModalLabel" aria-hidden="true">
            <div class="modal-dialog">
                <div class="modal-content">
                    <div class="modal-header">
                        <h1 class="modal-title fs-5" id="exampleModalLabel">部门管理</h1>
                        <button type="button" class="btn-close" data-bs-dismiss="modal" aria-label="Close"></button>
                    </div>
                    <form class="login-form">
                        <input type="hidden" class="change" name="id">
                        部门名称:<input type="text" name="name" class="change-"><br>
                    </form>
                    <div class="modal-footer">
                        <button type="button" class="btn btn-secondary" data-bs-dismiss="modal">关鸡</button>
                        <button type="button" class="btn btn-primary zouni2">走你</button>
                    </div>
                </div>
            </div>
        </div>
    </div>
    <script>
        //http://7b3866a3.r23.cpolar.top
        //获取body元素
        const body = document.querySelector('.body')
        function getmsg() {
            axios({
                url: 'http://7b3866a3.r23.cpolar.top/depts',
                method: 'GET'
            }).then(res => {
                const arr = res.data.data.map((item, index) => {
                    const { id, name, updateTime } = item
                    return `
                    <tr>
                        <td>${index + 1}</td>
                        <td class = 'namestr'>${name}</td>
                        <td>${updateTime}</td>
                        <td class="action-btn" data-id='${id}'>
                            <button class="edit-btn" data-bs-toggle="modal" data-bs-target="#exampleModal1">修改</button>
                            <button class="delete-btn">删除</button>
                            </td>
                            </tr>
                            `
                }).join('')
                body.innerHTML = arr
            })
        }
        getmsg()
        //新增数据
        document.querySelector('.modal .btn-primary').addEventListener('click', function () {
            const deptname = document.querySelector('.deptname').value
            axios({
                url: 'http://7b3866a3.r23.cpolar.top/depts',
                method: 'POST',
                data: {
                    name: deptname
                }
            }).then(res => {
                const modal = document.querySelector('#exampleModal')
                const newmodal = bootstrap.Modal.getInstance(modal)
                if (newmodal) {
                    newmodal.hide()
                }
                getmsg()
            })
        })
        //删除数据
        document.querySelector('.body').addEventListener('click', function (e) {
            if (!e.target.classList.contains('delete-btn')) {
                return
            }            // console.log(e.target.parentNode.dataset);
            const id = e.target.parentNode.dataset.id
            axios({
                url: 'http://7b3866a3.r23.cpolar.top/depts',
                method: 'DELETE',
                params: {
                    id
                }
            }).then(res => {
                console.log(res);
                getmsg()
            })
        })
        //查询部门
        document.querySelector('#exampleModal0 .btn-primary').addEventListener('click', function () {
            const id = document.querySelector('.id-').value
            axios({
                url: `http://7b3866a3.r23.cpolar.top/depts/${id}`,

            }).then(res => {
                if (!res.data.data) {
                    console.log('没有这个部门儿');
                    return
                }
                console.log(res.data.data);

            })
        })
        //修改信息
        document.querySelector('.body').addEventListener('click', function (e) {
            if (!e.target.classList.contains('edit-btn')) {
                return
            }            // console.log(e.target.parentNode.dataset);
            document.querySelector('#exampleModal1 .change-').value = e.target.parentNode.parentNode.children[1].innerHTML
            document.querySelector('#exampleModal1 .change').value = e.target.parentNode.dataset.id
        })
        document.querySelector('.modal .zouni2').addEventListener('click', function () {
            const data = serialize(document.querySelector('#exampleModal1 .login-form'),{hash:true,empty:true})
            const {id,name} = data
            console.log(id,name);
            
                axios({
                    url: 'http://7b3866a3.r23.cpolar.top/depts',
                    method: 'PUT',
                    data:{
                        id,
                        name
                    }
                }).then(res => {
                    const modal = document.querySelector('#exampleModal1')
                    const newmodal = bootstrap.Modal.getInstance(modal)
                    if (newmodal) {
                        newmodal.hide()
                    }
                    getmsg()
                })
            })
    </script>
</body>

</html>